package com.caszx.chargingpile.utils;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import com.caszx.chargingpile.R;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;

/**
 *
 */
public class DialogHelp {

    public interface DialogActionClickListener{
        public void onPositiveActionClicked(DialogFragment fragment);
        public void onNegativeActionClicked(DialogFragment fragment);
    }

    /**
     * 仅仅显示一个内容的Dialog
     *
     * */
    public static  void showDialogOnlyMesg(Object context, String msg, final DialogActionClickListener dialogActionClickListener){
        Dialog.Builder  builder = new SimpleDialog.Builder(R.style.SimpleDialogLight){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onNegativeActionClicked(fragment);
                }
            }
        };

        ((SimpleDialog.Builder)builder).message(msg)
                .positiveAction("确定")
                .negativeAction("取消");
        buildialog(context,builder);
    }

    public static  void showDialogOnlyMesg(Object context,String positive,String negative, String msg, final DialogActionClickListener dialogActionClickListener){
        Dialog.Builder  builder = new SimpleDialog.Builder(R.style.SimpleDialogLight){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onNegativeActionClicked(fragment);
                }
            }
        };


        ((SimpleDialog.Builder)builder).message(msg);
//                .positiveAction("确定")
//                .negativeAction("取消");

        if(positive!=null){
            builder.positiveAction(positive);
        }
        if(negative!=null){
            builder.negativeAction(negative);
        }
        buildialog(context,builder);
    }

/***
 * 仅仅显示一个标题的Dialog
 * */
    public static  void showDialogOnlyTitle(Object context,String title,final DialogActionClickListener dialogActionClickListener){
        Dialog.Builder  builder = new SimpleDialog.Builder(R.style.SimpleDialogLight){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onNegativeActionClicked(fragment);
                }
            }
        };

        builder.title(title)
                .positiveAction("确定")
                .negativeAction("取消");
        buildialog(context,builder);
    }



    /***
     * 显示一个标题和内容的Dialog
     * */
    public  static void showDialogTitleMeg(Object context,String msg,String title,final DialogActionClickListener dialogActionClickListener){
        Dialog.Builder  builder = new SimpleDialog.Builder(R.style.SimpleDialogLight){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                if(dialogActionClickListener!=null){
                    dialogActionClickListener.onNegativeActionClicked(fragment);
                }
            }
        };

        ((SimpleDialog.Builder)builder).message(msg)
                .title(title)
                .positiveAction("确定")
                .negativeAction("取消");
        buildialog(context,builder);
    }


    private  static void buildialog( Object context, Dialog.Builder builder){
        DialogFragment fragment = DialogFragment.newInstance(builder);
        if(context instanceof AppCompatActivity){
            fragment.show(((AppCompatActivity)context).getSupportFragmentManager(), null);
        }else if(context instanceof Fragment){
            fragment.show(((Fragment)context).getChildFragmentManager(), null);
        }
    }
}
